// pages/vip/vip.js
const app = getApp();
const url = app.globalData.url;
const WxParse = require('../../wxParse/wxParse.js');  
Page({
  /**
   * 页面的初始数据
   */
  data: {
    loding:true,
    cc:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },
  // 配置文件
  peizi: function () {
    let that = this;
    app.fetch('api/wx/getpeizi', { dlx: 'xcxvip' }, "GET", (err, data) => {
      console.log(data)
      if (data.code == 200) {
        if(data.data.xcxvip.title.txt){
          wx.setNavigationBarTitle({
            title: data.data.xcxvip.title.txt
          });
        }
        that.setData({
          peizi:data.data,
        });
        article_content: WxParse.wxParse('article', 'html', data.data.xcxvip.tqsm.txt, that, 0);
        that.hquser();
        that. getvip();
      } else {
        wx.showModal({
          title: '接口数据错误',
          content: '接口:"' + url + 'api/wx/getpeizi.php' + '",msg:' + data.msg,
          showCancel: false,//是否显示取消按钮
          confirmText: "好的",//默认是“确定”
          confirmColor: 'skyblue',//确定文字的颜色
          success: function (res) {

          },
          fail: function (res) { },//接口调用失败的回调函数
          complete: function (res) { },//接口调用结束的回调函数（调用成功、失败都会执行）
        })
      }
    })
  },
  // 获取user信息;
  hquser() {
    let that = this;
    wx.getStorage({
      key: 'openid',
      success(res) {
        app.fetch('api/wx/user', { openid: res.data }, "POST", (err, data) => {
          if (data.code == 200) {
            that.setData({ user: data.data[0] })
            // that.getcsData();

          }
        })
      }
    })
  },
  // 获取vip列表
  getvip(){
    let that = this;
    app.fetch('api/wx/getvip', {}, "POST", (err, data) => {
      console.log(data.vip);
      that.setData({loding:false,viplist:data.vip,ktvip:data.vip[0]});
    })
  },
  // 选择会员
  xzzf(e){
    // console.log(e.currentTarget.dataset.index);
    let i = e.currentTarget.dataset.index;
    let that=this;
    let vip=that.data.viplist;
    that.setData({ktvip:vip[i],cc:i});
    
  },
  // 支付请求
  qrzf() {
    let that = this;
    let user = that.data.user;
    let fee = that.data.ktvip.jb;
    if(user.jb-fee>=0){
      that.czjb();
      console.log(1)
    }else{
      wx.showModal({
        title: '提示',
        content: that.data.peizi.xcx.jb.txt+"余额不足,请先充值",
        showCancel: true,//是否显示取消按钮
        cancelText: "不充值",//默认是“取消”
        cancelColor: 'skyblue',//取消文字的颜色
        confirmText: "充值",//默认是“确定”
        confirmColor: 'red',//确定文字的颜色
        success: function (res) {
          if (res.cancel) {
            //点击取消,默认隐藏弹框
          } else {
            //点击确定
            that.toqb();
          }
        },
        fail: function (res) { },//接口调用失败的回调函数
        complete: function (res) { },//接口调用结束的回调函数（调用成功、失败都会执行）
      })
    }
  },
  czjb(){
    console.log(2)
    let that = this;
    let sj={
      uid: that.data.user.uid,
      sz: 2,
      jb: that.data.ktvip.jb,
      tt: '购买'+that.data.viplist[that.data.cc].title,
      lx:'xcx'
    };
    app.fetch('api/wx/czjb', sj, "POST", (err, data) =>{
      console.log(data)
        if(data.code==200){
          that.gmvip();
        }
    })
  },
  gmvip:function(){
    let that = this;
    let list = [];
    that.setData({ 'ktvip.uid': that.data.user.uid});
    console.log(JSON.stringify(that.data.ktvip));
    console.log(that.data.ktvip);
    app.fetch('api/wx/ktvip', { ktvip: JSON.stringify(that.data.ktvip)}, "POST", (err, data) =>{
      console.log(data)
        if(data.code==200){
          wx.showToast({
            title: '开通成功',
            icon: 'success',
            duration: 1000
          });
          setTimeout(function () {
            wx.hideLoading();
            that.peizi();
            wx.navigateBack({
              data:1
            })
          }, 1000)
        }
    })
  },
  toqb(){
    wx.navigateTo({
      url: "/pages/qianb/qianb",
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.peizi();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})